<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>在线客服</title>
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.3/css/bootstrap.css" rel="stylesheet">
    <style media="screen">
        .m-crm {
            max-width: 400px;
            margin: 2rem auto 2rem;
        }
    </style>
</head>

<body>
	<!-- <div class="m-crm">
	    <button type="button" class="btn btn-default btn-lg btn-block" onclick="openSdk()">Open接入方式</button>
	    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="logoff()">logoff</button>
	</div> -->
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<!-- <script src="./config.js" type="text/javascript"></script> -->
<script type="text/javascript">
	// var baseURL = 'http://39.97.67.66:39014/youxuegou/api/v1/' // 上线地址
	var baseURL = 'https://test.yoxuegou.com/youxuegou/api/v1/' // 上线地址
	var id = '' // 参数id
	var token = '' // 用户token
	var chatData = '' // 聊天数据
	
	 window.addEventListener('pageshow', function (e) {
	     if (e.persisted) {
			 // alert(e.persisted)
	         window.location.reload()
	     }
	 })
	
	// 转化请求参数
	function getQuery() {
		var obj = {};
		var query = window.location.search.slice(1);
		var queryArr = query.split("&");
		for (var i = 0; i < queryArr.length; i++) {
			var temp = queryArr[i].split('=');
			obj[temp[0]] = decodeURI(temp[1]);
		}
		console.log("obj",obj)
		id = obj.id;
		token = obj.token
		ajax()
	}
	// alert(localStorage.getItem('isBack'))
	// 是否返回
	back()
	// 获取参数
	getQuery()
	
	function back() {
		 var isBack =  sessionStorage.getItem('isBack')
		 console.log("isBack",isBack)
		 if (isBack == 1) {
			 sessionStorage.setItem("isBack", '');
			 wx.miniProgram.navigateBack();
			 // wx.miniProgram.switchTab({
			 // 	url:'/pages/tabBar/index/index',
				// success() {
				// }
			 // });
		 } else {
			 sessionStorage.setItem("isBack", '1');
		 }
	}
	
	// 请求
	function ajax(){
		// debugger
		var xhr = new XMLHttpRequest();
		xhr.open("GET", baseURL+ 'kf/getKfData?id='+id);
		// xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8");
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		// xhr.setRequestHeader('X-Access-Token','eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtb2JpbGUiOiJtZW1iZXJVc2VyQDEzMTQwNTIwMTUyLDAiLCJleHAiOjE1ODQ1OTc0MDR9.Ek28BW64aIN1de9IpgrHhzwE7OD72VMPeNEiznCtDM8');
		xhr.setRequestHeader('X-Access-Token',token);
		// let data = {"id":id}
		// 除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify，服务端语言也都有处理 JSON 的函数，使用 JSON 不会遇上什么麻烦
		  // data=JSON.stringify(data);
		  // xhr.send(data);
		  xhr.send(null);
		// xhr.send({"id":jsonList.id,"status":jsonList.status});
		xhr.onreadystatechange = function(){
		    if(xhr.readyState == 4 && xhr.status == 200){
				console.log("xhr.responseText1",xhr.responseText)
				if (JSON.parse(xhr.responseText).code == 1) {
					chatData = JSON.parse(xhr.responseText).data
				}
				console.log('xhr.responseText',JSON.parse(xhr.responseText))
				
		    }
		}
	}
	
	
    (function (w, d, n, a, j) {
        w[n] = w[n] || function () {
            (w[n].a = w[n].a || []).push(arguments)
        };
        j = d.createElement('script');
        j.async = true;
        j.src = 'https://qiyukf.com/script/fdd795ade6386e5d64301cbc7264c7db.js';
		// j.src = 'https://qiyukf.com/script/4ba53a88957fb27300d38fde30e0dc0f.js';//ysftest4
        d.body.appendChild(j);
    })(window, document, 'ysf');
	
	 var isSdkReady = false;
	
	ysf('onready', function () {//SDK初始化成功后开启聊天窗口
		
		 // ysf('product', {
		 // 	show: 1, // 1为打开， 其他参数为隐藏（包括非零元素）
		 // 	title: chatData.goods.title,
		 // 	desc: chatData.goods.desc,
		 // 	picture: chatData.goods.picture,
		 // 	note: chatData.goods.note,
		 // 	sendByUser: 0
		 // }),
		 // alert(JSON.stringify(chatData.kfcrmvo))
		 ysf('config', {
		 	// uid: "wly123123",
		    uid: chatData.kfcrmvo.uid,
		 	name: chatData.kfcrmvo.name,          
		 	mobile: chatData.kfcrmvo.mobile,
		 	data: chatData.kfcrmvo.data,
			bid: chatData.kfcrmvo.bid,
		 	success: function(){   // 成功回调
		 	// location.href = ysf('url');
			if (chatData.hasGoods == 1) {
					// product()
			} else {
				 location.href = ysf('url');
				// ysf('open');
			}
		 	},
		 	error: function(){       // 错误回调
		 		ysf('error');
		 	}
			if (chatData.hasGoods == 1) {
				ysf('product', {
					show: chatData.goods.show, // 1为打开， 其他参数为隐藏（包括非零元素）
					title: chatData.goods.title,
					desc: chatData.goods.desc,
					picture: chatData.goods.picture,
					note: chatData.goods.note,
					success: function(){   // 成功回调
					alert(111)
					 location.href = ysf('url');
						// ysf('open');
					},
					error: function(){       // 错误回调
					}
				})
			}
			
		 })
	})
	
	// 初始化商品卡片，自动发送
	function product() {
		ysf('product', {
			show: chatData.goods.show, // 1为打开， 其他参数为隐藏（包括非零元素）
			title: chatData.goods.title,
			desc: chatData.goods.desc,
			picture: chatData.goods.picture,
			note: chatData.goods.note,
			success: function(){   // 成功回调
			 location.href = ysf('url');
				// ysf('open');
			},
			error: function(){       // 错误回调
			}
		})
	}

   
</script>
</body>
</html>